using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;

namespace DataSync.Utilities
{
    class SystemTray
    {
        private NotifyIcon SysTrayIcon;
        private System.Windows.Forms.Timer m_AnimTimer;
        private int _NTimeOut;
        private bool bIsTimeOut;
        private Icon [] AnimIconList;
        private bool bAnimate;

        public SystemTray()
        {
            InitDefaults();
        }
        public SystemTray(string pTrayText)
        {
            InitDefaults();
            SysTrayIcon.Text = pTrayText;
        }

        public SystemTray(string pTrayText, Icon pIcon)
        {
            InitDefaults();
            SysTrayIcon.Text = pTrayText;
            SysTrayIcon.Icon = pIcon;
        }

        public SystemTray(string pTrayText,Icon pIcon,bool pAnimate)
        {
            InitDefaults();
            SysTrayIcon.Text = pTrayText;
            SysTrayIcon.Icon = pIcon;
            bAnimate = pAnimate;
        }

        private void InitDefaults()
        {
            SysTrayIcon = new NotifyIcon();
            m_AnimTimer = new System.Windows.Forms.Timer();
            //Set Handler
            m_AnimTimer.Tick += SysTrayAnimator;
            SysTrayIcon.Visible = true;
            bAnimate = false;
        }

        public void Dispose()
        {
            SysTrayIcon.Dispose();
            m_AnimTimer.Dispose();
        }

        public Icon Icon
        {
            get
            {
                return SysTrayIcon.Icon;
            }
            set
            {
                SysTrayIcon.Icon = value;
            }
        }
 
        public bool Visible
        {
            get
            {
                 return SysTrayIcon.Visible; 
            }
            set
            {
                SysTrayIcon.Visible = value;
            }
        }
        public string TrayText
        {
            get
            {
                return SysTrayIcon.Text;
            }
            set
            {
                SysTrayIcon.Text = value;
            }
        }

        public bool Animate
        {
            get
            {
                return bAnimate;
            }
            set
            {
                bAnimate = value;
            }
        }

        public string BaloonTipText
        {
            get
            {
                return SysTrayIcon.BalloonTipText;
            }
            set
            {
                SysTrayIcon.BalloonTipText = value;
            }
        }

        public string BalloonTipTitle
        {
            get
            {
                return SysTrayIcon.BalloonTipTitle;
            }
            set
            {
                SysTrayIcon.BalloonTipTitle = value;
            }
        }

        public ToolTipIcon BalloonTipIcon
        {
            get
            {
                return SysTrayIcon.BalloonTipIcon;
            }
            set
            {
                SysTrayIcon.BalloonTipIcon = value;
            }
        }

        public void SetContextMenuOrStrip(object value)
        {
                if (value.GetType().Name == "ContextMenu")
                {
                    SysTrayIcon.ContextMenu = (ContextMenu)value;
                }

                if (value.GetType().Name == "ContextMenuStrip")
                {
                    SysTrayIcon.ContextMenuStrip = (ContextMenuStrip)value;
                }
        }
        
        public ContextMenu ContextMenu
        {
            get
            {
                return SysTrayIcon.ContextMenu;
            }
            set
            {
                SysTrayIcon.ContextMenu = value;
            }
        }
        
        public ContextMenuStrip ContextMenuStrip
        {
            get
            {
                return SysTrayIcon.ContextMenuStrip;
            }
            set
            {
                SysTrayIcon.ContextMenuStrip = value;
            }
        }  

        public void StartAnimation()
        {
            if(bAnimate)
                m_AnimTimer.Start();
        }

        public void StopAnimation()
        {
            if (bAnimate)
                m_AnimTimer.Stop();
        }

        public void StartAnimation(int timeOut)
        {
            if (!bAnimate) 
                return;

            bIsTimeOut = true;
            _NTimeOut = timeOut;
            m_AnimTimer.Start();
        }

        public void ShowBaloonTrayTip(int nTimeout)
        {
            SysTrayIcon.ShowBalloonTip(nTimeout);
        }

        public void ShowBaloonTrayTip(int nTimeOut, string tipTitle,string tipText,ToolTipIcon tipIcon)
        {
            SysTrayIcon.ShowBalloonTip(nTimeOut, tipTitle, tipText, tipIcon);
        }

        public void SetIconRange(object[] IconList)
        {
            var tp = IconList[0].GetType();
            //if(tp.Name == "String")
            //{
                AnimIconList = new Icon[IconList.Length];
                for (var i = 0; i < IconList.Length; ++i)
                {
                    //AnimIconList[i] = new Icon(IconList[i].ToString());
                    AnimIconList[i] = IconList[i] as Icon;
                }
                
            //}
            if (tp.Name == "Icon")
            {
                AnimIconList = new Icon[IconList.Length];
                for (var i = 0; i < IconList.Length; ++i)
                {
                    AnimIconList[i] = (Icon)IconList[i];
                }
            }
            if (IconList.Length > 0)
                Icon = AnimIconList[0];
        }

        private void SysTrayAnimator(object sender, EventArgs e)
        {
            if (bIsTimeOut)
            {
                var i = 0;
                while (_NTimeOut > 175)
                {
                    if(i >= AnimIconList.Length)
                    {
                        i = 0;
                    }
                    SysTrayIcon.Icon = AnimIconList[i++];
                    _NTimeOut -= 175;
                    Thread.Sleep(175);
                }
                m_AnimTimer.Stop();
                bIsTimeOut = false;
            }
            else
            {
                for (var i = 0; i < AnimIconList.Length; ++i)
                {
                    SysTrayIcon.Icon = AnimIconList[i];
                    Thread.Sleep(175);
                }
            }
        }
    }
}
